//
//  ConvenienceViewController.m
//  PeopleStreet
//
//  Created by 刘建峰 on 16/7/1.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "ConvenienceViewController.h"
#import "ConvenienceTableViewCell.h"
static NSString * cellIndentifer = @"ConvenienceTableViewCell";
@interface ConvenienceViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *navView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic,strong)NSMutableArray * numbersArray;
@end

@implementation ConvenienceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initViewLayout];
    [self getPhoneNumberList];
    // Do any additional setup after loading the view.
}

- (void)initViewLayout{
    [self adaptiveViewLayout:self.view];
    [self adaptiveViewLayout:self.navView];
    
    [_tableView registerNib:[UINib nibWithNibName:cellIndentifer bundle:nil] forCellReuseIdentifier:cellIndentifer];
    _tableView.dataSource = self;
    _tableView.delegate = self;
}


- (void)getPhoneNumberList{

    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:SX_getHandyServiceList withHttpType:@"POST" withTag:SX_SX_getHandyServiceList];
}


- (IBAction)back:(id)sender {
    [self.navigationController  popViewControllerAnimated:YES];
}


#pragma mark -- UITableViewDataSource

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 25*VerticalRatio();
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * view = [[UIView alloc]initWithFrame:flexibleFrame(CGRectMake(0, 0, 320, 25), NO)];
    view.backgroundColor = [UIColor colorWithWhite:0.957 alpha:1.000];
    UILabel * label = [[UILabel alloc]initWithFrame:flexibleFrame(CGRectMake(14, 0, 300, 25), NO)];
    label.textColor = [UIColor colorWithWhite:0.502 alpha:1.000];
    label.font = [UIFont systemFontOfSize:14*VerticalTexeRatio()];
    NSDictionary * dic = _numbersArray[section];
    label.text = [Common stringFromData:dic[@"hstyName"]];
    [view addSubview:label];
    return view;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _numbersArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return 50*VerticalRatio();
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray * array = _numbersArray[section][@"handyServicePhone"];
    return array.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ConvenienceTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifer];
    if (cell == nil) {
        cell = [[ConvenienceTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifer];
    }
    NSDictionary * dic = _numbersArray[indexPath.section][@"handyServicePhone"][indexPath.row];
    cell.titleLabel.text = [Common stringFromData:dic[@"hsphName"]];
    cell.phoneLabel.text = [Common stringFromData:dic[@"hsphPhone"]];
        return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [Common callPhone:[Common stringFromData:_numbersArray[indexPath.section][@"handyServicePhone"][indexPath.row][@"hsphPhone"]] view:self.view];
}

#pragma   mark---- GetADBanner Request Delegate

-(void)fetchDatabaseFinished:(NSMutableDictionary *)database withTag:(NSInteger)tag{
    if (tag==SX_SX_getHandyServiceList){
        
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            _numbersArray = database[@"result"];
            [_tableView reloadData];
        }
    }
}

-(void)fetchDatabaseFailed:(NSError *)error message:(NSString *)message{
    
    //
    [self dismissWaitDialog];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
